home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-11-25 | 27.7 KB | 1,089 lines | [TEXT/gamI] |
- -*-Indented-Text-*-
-
- This file contains the examples, taken directly from the Dylan manual.
- This file is not in an executable format.
-
-
- Page 27
-
- ? "abc"
- "abc"
- ? 123
- 123
- ? foo:
- foo:
- ? #\a
- #\a
- ? #t
- #t
- ? #f
- #f
- ? (quote foo)
- foo
- ? 'foo
- foo
- ? '(1 2 3)
- (1 2 3)
-
-
- Page 28-29
-
- ? <window>
- {the class <window>}
- ? concatenate
- {the generic function concatenate}
- ? (define my-variable 25)
- my-variable
- ? my-variable
- 25
- ? (bind ((x 50))
- (+ x x))
- 100
- ? (setter element)
- {the generic function (setter element)}
- ? (define (setter my-variable) 20)
- (setter my-variable)
- ? (setter my-variable)
- 20
-
- Page 29
-
- ? (+ 3 4)
- 7
- ? (* my-variable 3)
- 75
- ? (* (+ 3 4) 5)
- 35
- ? ((if #t + *) 4 5)
- 9
-
- Page 30
-
- ; Creates and initializes a module variable
- (define my-variable 25)
- ; Sets the value to 12
- (set! my-variable 12)
- ; Returns 30. Uses lexical variables x and y.
- (bind ((x 10) (y 20))
- (+ x y))
- ; Creates an anonymous method, which expects 2
- ; numeric arguments.
- (method ((a <number>) (b <number>))
- (list (- a b) (+ a b)))
-
- Page 30
-
- ? (values 1 2 3)
- 1
- 2
- 3
- ? (define-method edges ((center <number>)(radius <number>))
- (values (- center radius) (+ center radius)))
- edges
- ? (edges 100 2)
- 98
- 102
-
- Page 32
-
- ? foo
- error: unbound variable foo
- ? (define foo 10)
- foo
- ? foo
- 10
- ? (+ foo 100)
- 110
- ? bar
- error: unbound variable bar
- ? (define bar foo)
- bar
- ? bar
- 10
- ? (define foo 20)
- warning: redefining variable foo
- ? foo
- 20
- ? bar
- 10
- ? (+ foo bar)
- 30
-
- Page 33
-
- ? (bind ((number1 20))
- (number2 30))
- (+ number1 number2))
- 50
-
- Page 33
-
- ? (bind ((x 20)
- (y (+ x x)))
- (+ y y))
- 80
-
- Page 33
-
- ? (define foo 10)
- foo
- ? (+ foo foo)
- 20
- ? (bind ((foo 35))
- (+ foo foo))
- 70
- ? (bind ((foo 20))
- (bind ((foo 50))
- (+ foo foo)))
- 100
-
- Page 34
-
- ? (bind (((x <integer>) (sqrt 2)))
- x)
- error: 1.4142135623730951 is not an instance of <integer>
-
-
- Page 34
-
- ? (bind ((foo bar baz (values 1 2 3)))
- (list foo bar baz))
- (1 2 3)
- ? (define-method opposite-edges ((center <number>)
- (radius <number>))
- (bind ((min max (edges center radius)))
- (values max min)))
- opposite-edges
- ? (opposite-edges 100 2)
- 102
- 98
-
- Page 34
-
- ? (bind ((x 10)
- (y 20))
- (bind ((x y (values y x)))
- (list x y)))
- (20 10)
-
- Page 34
-
- ? (bind ((#rest nums (edges 100 2)))
- nums)
- (98 102)
-
- Page 41
-
- ? (double 10)
- error: unbound variable double.
-
- Page 41
-
- ? (define-method double ((thing <number>))
- (+ thing thing))
- double
- ? double
- {the generic function double}
- ? (double 10)
- 20
-
- Page 41
-
- ? (double "the rain in Spain.")
- error: no method for {the generic function double} was found
- for the arguments ("the rain in Spain.")
-
- Page 41
-
- ? (define-method double ((thing <sequence>))
- (concatenate thing thing))
- double
- ? (double "the rain in Spain.")
- "the rain in Spain.the rain in Spain."
- ? (double '(a b c))
- (a b c a b c)
-
- Page 43
-
- ? (define-method show-rest (a #rest b)
- (print a)
- (print b)
- #t)
- show-rest
- ? (show-rest 10 20 30 40)
- 10
- (20 30 40)
- #t
- ? (show-rest 10)
- 10
- ()
- #t
-
- Page 44
-
- (define-method percolate (#key (brand 'maxwell-house)
- (cups 4)
- (strength 'strong))
- (make-coffee brand cups strength))
- (define-method layout (widget #key (position: the-pos)
- (size: the-size))
- (bind ((the-sibling (sibling widget)))
- (unless (= the-pos (position the-sibling))
- (align-objects widget the-sibling the-pos the-size))
-
- Page 44
-
- (percolate brand: 'folgers cups: 10)
- (percolate strength: 'weak
- brand: 'tasters-choice
- cups: 1)
- (layout my-widget position: (point 10 10)
- size: (point 30 50))
- (layout my-widget size: (query-user-for-size))
-
- Page 45
-
- ? (define-method show-keys (req1 req2 #key foo)
- (format #t "requireds: ~a ~a~%" req1 req2)
- (format #t "key: ~a" foo)
- #t)
- show-keys
- ? (show-keys 'one 'two foo: 'three)
- requireds: one two
- key: three
- #t
- ? (show-keys foo: 'three)
- requireds: foo: three
- key: #f
- #t
-
- Page 46
-
- ? (define-method label ((x <object>) #key price)
- (list price x))
- label
- ? (define-method label ((x <sequence>) #key unit-price)
- (add x (* unit-price (length x))))
- label
- ? (define-method label ((x <list>) #rest info #key calories)
- (add x calories))
- label
- ? (label 'grape price: 189 unit-price: 2)
- error: illegal keyword argument unit-price:. Accepted keyword arguments are (price:).
- ? (label 'grape price: 189)
- (189 grape)
- ? (label (vector 3 4 5) price: 189 unit-price: 2)
- #(6 3 4 5)
- ? (label (vector 3 4 5) protein: 7 fat: 8 calories: 9)
- error: illegal keyword argument protein:. Accepted keyword arguments are (price: unit-price:).
- ? (label (list 3 4 5) protein: 7 fat: 8 calories: 9)
- (9 3 4 5)
-
- Page 46
-
- ? (define-method test (the-req #rest the-rest
- #key a b)
- (print the-req)
- (print the-rest)
- (print a)
- (print b))
- test
- ? (test 1 a: 2 b: 3 c: 4)
- 1
- (a: 2 b: 3 c: 4)
- 2
- 3
-
- Page 49
-
- (define-class <point> (<object>)
- horizontal
- vertical)
-
- Page 49
-
- (horizontal my-point)
-
- Page 49
-
- ((setter horizontal) my-point 10)
-
- Page 50
-
- (set! (horizontal my-point) 10)
-
- Page 51
-
- ? (define-class <menu> (<object>)
- title
- action)
-
- Page 55
-
- ? (define-class <rectangle> (<object>)
- (top type: <integer>
- init-value: 0
- init-keyword: top:)
- (left type: <integer>
- init-value: 0
- init-keyword: left:)
- (bottom type: <integer>
- init-value: 100
- init-keyword: bottom:)
- (right type: <integer>
- init-value: 100
- init-keyword: right:))
- <rectangle>
- ? <rectangle>
- {the class <rectangle>}
- ? (define my-rectangle (make <rectangle> top: 50 left: 50))
- my-rectangle
- ? (top my-rectangle)
- 50
- ? (bottom my-rectangle)
- 100
- ? (set! (bottom my-rectangle) 55)
- 55
- ? (bottom my-rectangle)
- 55
- ? (set! (bottom my-rectangle) 'foo)
- error: foo is not an instance of <integer> while executing (setter bottom).
-
-
- Page 58
-
- (define-class <view> (<object>)
- (position allocation: instance)
- ...)
-
- (define-class <displaced-view> (<view>)
- (position allocation: virtual)
- ...)
-
- (define-method position ((v <displaced-view>))
- (displace-transform (next-method v)))
-
- (define-method (setter position) ((v <displaced-view>)
- new-position)
- (next-method v (undisplace-transform new-position)))
-
- Page 59
-
- (define-class <shape> (<view>)
- (image allocation: virtual)
- (cached-image allocation: instance init-value: #f)
- ...)
-
- (define-method image ((shape <shape>))
- (or (cached-image shape)
- (set! (cached-image shape) (compute-image shape))))
-
- (define-method (setter image) ((shape <shape>) new-image)
- (set! (cached-image shape) new-image))
-
- Page 61
-
- ? (define foo 10)
- 10
- ? foo ;this is a variable
- 10 ;this is the variable's contents
- ? (set! foo (+ 10 10))
- 20
- ? foo
- 20
- ? (setter element) ;this is a variable
- {generic function (setter element)} ;the variable's contents
- ? (set! (setter element) %set-element)
- {primitive function %set-element}
- ? (id? (setter element) %set-element)
- #t
-
- Page 62
-
- ? (define foo (vector 'a 'b 'c 'd))
- foo
- ? foo
- #(a b c d)
- ? (element foo 2)
- c
- ? (set! (element foo 2) 'sea)
- sea
- ? (element foo 2)
- sea
- ? foo
- #(a b sea d)
-
- Page 64
-
- ? (define-method test ((thing <object>))
- (if thing
- #t
- #f))
- test
- ? (test 'hello)
- #t
- ? (test #t)
- #t
- ? (test #f)
- #f
-
- ? (define-method double-negative ((num <number>))
- (if (< num 0)
- (+ num num)
- num))
- double-negative
- ? (double-negative 11)
- 11
- ? (double-negative -11)
- -22
-
- Page 65
-
- ? (define-method show-and-tell ((thing <object>))
- (if thing
- (begin
- (print thing)
- #t)
- #f))
- show-and-tell
- ? (show-and-tell "hello")
- hello
- #t
-
- Page 65
-
- (when (bonus-illuminated? pinball post)
- (add-bonus-score current-player 100000))
-
- Page 65
-
- (unless (detect-gas? nose)
- (light match))
-
- Page 66
-
- (cond ((< new-position old-position)
- "the new position is less")
- ((= new-position old-position)
- "the positions are equal")
- (else: "the new position is greater"))
-
- Page 67
-
- (case (career-choice student)
- ((art music drama)
- (print "Don't quit your day job."))
- ((literature history linguistics)
- (print "That really is fascinating."))
- ((science math engineering)
- (print "Say, can you fix my VCR?"))
- (else: "I wish you luck."))
-
- Page 67
-
- (select my-object instance?
- ((<window> <view> <rectangle>) "it's a graphic object")
- ((<number> <list> <sequence>) "it's something computational")
- (else: "Don't know what it is"))
-
- Page 68
-
- ? (if #t
- (print "it was true")
- #t
- #f)
- error: too many arguments to if.
- ? (if #t
- (begin (print "it was true")
- #t)
- #f)
- "it was true"
- #t
-
- Page 69
-
- (define-method factorial ((n <integer>))
- (for ((i n (- i 1)) ;variable clause 1
- (v 1 (* v i))) ;variable clause 2
- ((<= i 0) v)) ;end test and result
-
- Page 69
-
- (define-method first-even ((s <sequence>))
- (for-each ((number s))
- ((even? number) number)
- ; No body forms needed
- ))
-
- Page 70
-
- (define-method schedule-olympic-games ((cities <sequence>)
- (start-year <number>))
- (for-each ((year (range from: start-year by: 4))
- (city cities))
- () ; No end test needed.
- (schedule-game city year)))
-
- Page 70
-
- ? (begin
- (dotimes (i 6) (print "bang!"))
- (print "click!"))
- bang!
- bang!
- bang!
- bang!
- bang!
- bang!
- click!
-
- Page 71
-
- ? (define-method first-even ((seq <sequence>))
- (bind-exit (exit)
- (do (method (item)
- (when (even? item)
- (exit item)))
- seq)))
- first-even
- ? (first-even '(1 3 5 4 7 9 10))
- 4
-
- Page 72
-
- ? +
- {the generic function +}
- ? '+
- +
- ? (quote +)
- +
- ? ''+
- (quote +)
- ? (+ 10 10)
- 20
- ? '(+ 10 10)
- (+ 10 10)
- ? (quote (+ 10 10))
- (+ 10 10)
-
- Page 73
-
- ? (apply + 1 '(2 3))
- 6
- ? (+ 1 2 3)
- 6
- ? (define math-functions (list + * / ))
- math-functions
- ? math-functions
- ({method +} {method *} {method /} {method })
- ? (first math-functions)
- {method +}
- ? (apply (first math-functions) 1 2 '(3 4))
- 10
-
- Page 79
-
- ? (method (num1 num2)
- (+ num1 num2))
- {an anonymous method}
-
- Page 80
-
- ;the second argument to SORT is the test function
- ? (sort person-list
- (method (person1 person2)
- (< (age person1)
- (age person2))))
- ? (bind ((double (method (number)
- (+ number number))))
- (double (double 10)))
- 40
-
- Page 80
-
- ? (define-method double ((my-method <function>))
- (method (#rest args)
- (apply my-method args)
- (apply my-method args)
- #f))
- double
- ? (define print-twice (double print))
- print-twice
- ? print-twice
- {an anonymous method}
- ? (print-twice "The rain in Spain. . .")
- The rain in Spain. . .The rain in Spain. . .
- #f
- ? (print-twice 55)
- 5555
- #f
-
- Page 81
-
- ? (define-method root-mean-square ((s <sequence>))
- (bind-methods ((average (numbers)
- (/ (reduce1 + numbers)
- (length numbers)))
- (square (n) (* n n)))
- (sqrt (average (map square s)))))
- root-mean-square
- ? (root-mean-square '(5 6 6 7 4))
- 5.692099788303083
-
- Page 81
-
- ? (define-method newtons-sqrt (x)
- (bind-methods ((sqrt1 (guess)
- (if (close? guess)
-